const app = getApp();

Page({
  data: {
    phone: '',
    code: '',
    nickname: '',
    gender: '',
    birthYear: '',
    datingType: '',
    codeSent: false,
    countdown: 60,
    registering: false
  },

  // 手机号输入
  onPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    });
  },

  // 验证码输入
  onCodeInput(e) {
    this.setData({
      code: e.detail.value
    });
  },

  // 昵称输入
  onNicknameInput(e) {
    this.setData({
      nickname: e.detail.value
    });
  },

  // 选择性别
  selectGender(e) {
    const gender = e.currentTarget.dataset.gender;
    this.setData({
      gender: gender
    });
  },

  // 选择出生年份
  onBirthYearChange(e) {
    this.setData({
      birthYear: e.detail.value
    });
  },

  // 选择交友类型
  selectDatingType(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      datingType: type
    });
  },

  // 发送验证码
  sendCode() {
    if (!this.data.phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return;
    }

    if (!/^1[3-9]\d{9}$/.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      });
      return;
    }

    // 模拟发送验证码
    wx.showToast({
      title: '验证码已发送',
      icon: 'success'
    });

    this.setData({
      codeSent: true
    });

    this.startCountdown();
  },

  // 开始倒计时
  startCountdown() {
    const timer = setInterval(() => {
      if (this.data.countdown > 0) {
        this.setData({
          countdown: this.data.countdown - 1
        });
      } else {
        clearInterval(timer);
        this.setData({
          codeSent: false,
          countdown: 60
        });
      }
    }, 1000);
  },

  // 注册
  register() {
    if (!this.validateForm()) {
      return;
    }

    this.setData({
      registering: true
    });

    // 模拟注册API调用
    setTimeout(() => {
      const userInfo = {
        token: 'mock_token_' + Date.now(),
        phone: this.data.phone,
        nickname: this.data.nickname,
        gender: this.data.gender,
        birthYear: this.data.birthYear,
        datingType: this.data.datingType,
        isRealNameAuth: false,
        isMember: false,
        profile: {
          nickname: this.data.nickname,
          gender: this.data.gender,
          birthYear: this.data.birthYear,
          datingType: this.data.datingType,
          age: new Date().getFullYear() - parseInt(this.data.birthYear),
          avatar: '/images/avatar/default.jpg',
          city: '',
          income: '',
          description: ''
        }
      };

      app.login(userInfo);

      this.setData({
        registering: false
      });

      wx.showToast({
        title: '注册成功',
        icon: 'success'
      });

      // 跳转到完善资料页面
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/edit-profile/edit-profile'
        });
      }, 1500);
    }, 2000);
  },

  // 验证表单
  validateForm() {
    if (!this.data.phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return false;
    }

    if (!/^1[3-9]\d{9}$/.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return false;
    }

    if (this.data.code.length !== 6) {
      wx.showToast({
        title: '验证码格式不正确',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.nickname) {
      wx.showToast({
        title: '请输入昵称',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.gender) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.birthYear) {
      wx.showToast({
        title: '请选择出生年份',
        icon: 'none'
      });
      return false;
    }

    if (!this.data.datingType) {
      wx.showToast({
        title: '请选择交友类型',
        icon: 'none'
      });
      return false;
    }

    return true;
  },

  // 去登录
  goToLogin() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  }
}); 